<?php
/***
* 基础信息控制器
* 包含头信息的封装，基本的错误、正确信息的返回
* 总体的数据结构有header、data组成
* header里面放基本信息
* data放具体的数据
* 请求：header里面放来源、版本
***/
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function __construct() {
        parent::__construct();
    }

    /***
    * 返回值的结构
    ***/
    public function result($data) {
    	   header('Content-Type:application/json; charset=utf-8');
		    //  $PROTOCOL_INFO = array( "version" => "1.0" );
		    //  $HEADER_INFO = array( "protocol" => $PROTOCOL_INFO, "timestamp" => time() );
	      //  $RESULT = array( "header" => $HEADER_INFO, "data" => $data);
         $RESULT = $data;
    	   exit(json_encode($RESULT));
    }

    public function success($data = array()) {
    	$data["code"] = $data["code"]?$data["code"]:'1000';
    	$data["msg"] = $data['msg']?$data['msg']:"执行成功";
    	$this->result($data);
    }

    public function error($data = array()) {
    	$data["code"] = $data["code"]?$data["code"]:'999';
    	$data["msg"] = $data['msg']?$data['msg']:"执行失败";
    	$this->result($data);
    }
    //判断是否为空
    public function is_nullAction($a) {
      if ($a == null) {
        $data['msg'] = '参数为空';
        $data['code'] = 999;
        $this->error($data);
      }
    }
    //利用token获取用户ID
    public function getMemberID($token=0){
    	if($token == 0){
    		$token = I('token');
    	}
    	if($token){
    		$memberToken = M('users');
    		$memberId = $memberToken->where(array('token'=>$token))->getField('uid');
    		if($memberId){
    			return $memberId;
    		}else{
    			return false;
    			//$ret['msg'] = 'token过期!';
    			//$this->error($ret);
    		}
    	}else{
    		return false;
    		//$ret['msg'] = '缺少token!';
    		//$this->error($ret);
    	}

    }




}
